# 下载文件

import zipfile
import os

from pyhanlp.static import download, remove_file


def test_data_path():
    """
    获取测试数据路径
    """
    data_path = os.path.join(os.getcwd(), "demo/testdata")
    if not os.path.isdir(data_path):
        os.mkdir(data_path)
    return data_path


def ensure_data(data_name, data_url):
    root_path = test_data_path()
    dest_path = os.path.join(root_path, data_name)
    if os.path.exists(dest_path):
        return dest_path
    if data_url.endswith(".zip"):
        dest_path += ".zip"
    download(data_url, dest_path)
    if data_url.endswith(".zip"):
        with zipfile.ZipFile(dest_path, "r") as archive:
            archive.extractall(root_path)
        remove_file(dest_path)
        dest_path = dest_path[: -len(".zip")]
    return dest_path
